<?
    cria_variaveis($_POST,$_GET,$_FILES);
    require_once("./classes/upload.php"); 
?>
<p class="titulo" align="center"><?php echo $msg->get('LUDECOMP_MSG_02') ?></p>
<p class="subtitulo" align="center"><?php echo $msg->get('LUDECOMP_MSG_03') ?></p>
<p>
<div class="tabbar">
    <ul>
    <li><a href="index.php?inc=ludecomp.php&modo=material"<?php if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "material") echo " class=\"tabact\""; ?>><?php echo $msg->get('Material_Teorico') ?></a></li>
    <li><a href="index.php?inc=ludecomp.php&modo=exemplo"<?php if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "exemplo") echo " class=\"tabact\""; ?>><?php echo $msg->get('Exemplos_Prontos') ?></a></li>
    <li><a href="index.php?inc=ludecomp.php&modo=teclado"<?php if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "teclado") echo " class=\"tabact\""; ?>><?php echo $msg->get('Entrada_Teclado') ?></a></li>
    </ul>
</div>
<div id="cas">
<?php
if (!isset($Calcular)) {	
    if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "material"){ //pagina inicial
        $uploads = new Upload();
        $uploads->Lista("sistemas");
    }
    else if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "exemplo") { //leitura de dados via banco de dados - modulo de exemplos e exercicios
        include "exibe_prob_sela_ludecomp_xml.php"; // exibe menu de exemplos contidos na biblioteca de exemplos em xml
    }
    else if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "teclado") { //modulo de exercicios - tela inicial
        if ((strlen($dim)==0) | ($dim<=0))  {
            echo "<form method=post enctype=multipart/form-data action=index.php?inc=ludecomp.php&modo=teclado>";
            echo $msg->get('LUDECOMP_MSG_04');
            echo "<input type=text size='2' maxlength='2' name=dim value=".($dim).">";
            echo "<br><br>";
            echo "<input type=hidden name=executar value=sim>";
            echo "<input class=button type=submit name=Submit value=".$msg->get('G_MSG_02').">";
            echo "</form>"; 
            echo "<form method=post enctype=multipart/form-data action=index.php?inc=ludecomp.php>";
            echo "<input class=button type=submit name=Submit value=".$msg->get('G_MSG_03').">";
            echo "</form>";
        }
        elseif (!checaNumerico($dim))
        {
?>
            <p><font color="red"><?php echo $msg->get('Apenas_Dados_Numericos') ?></font></p>
            <p><input type="button" value="<?php echo $msg->get('Voltar') ?>" onclick="history.back()" class="button"></p>
<?php
        }
        else  {
            echo "<form method=post enctype=multipart/form-data action=index.php?inc=ludecomp.php&modo=teclado>";
            //echo LUDECOMP_MSG_04;
            echo "<input type=hidden name=dim value=".($dim).">";
            //echo "<br><br>";
            echo "<p align=left><font face=Arial>".$msg->get('LUDECOMP_MSG_05')."</font>";
            echo "<nobr>";
            for ($i=0; $i<$dim; $i++) {
                echo "<br><br>";
                for ($j=0; $j<$dim; $j++) {
                    $aux=$j+1;
                    echo "<input type=text maxlength=10 size=5 name=A[".($i)."][".($j)."] value=".($A[$i][$j])."> X <sub> ".($aux)." </sub>";
                    if ($j<($dim-1)) {
                        echo " + ";
                    } //end if
                } //end for j
                echo " = ";
                echo "<input type=text maxlength=10 size=5 name=T[".($i)."] value=".($T[$i]).">";
            } //end for i
            echo "</nobr>";
            echo "<p>";
            echo "<input class=button type=submit name=Calcular value=".$msg->get('G_MSG_04').">";
            echo "<br>";
            echo "</form>";
            echo "<form method=post enctype=multipart/form-data action=index.php?inc=ludecomp.php>";
            echo "<input class=button type=submit name=Submit value=".$msg->get('G_MSG_03').">";
            echo "</form>";
        } //end if
    }
} else {
	// armazena os dados do problema para serem resolvidos por outros metodos
	for ($i=0; $i<$dim; $i++) {
	   for ($j=0; $j<$dim; $j++) {
		$Atemp[$i][$j]=$A[$i][$j];
	   } //end for j
	   $Atemp[$i][$j]=$T[$i];
	} //end for i
    if ($modo != "teclado")
        verificaBib($xml_file);
    if (!checaNumerico($dim) || !checaNumerico($A))
    {
?>
        <p><font color="red"><?php echo $msg->get('Apenas_Dados_Numericos') ?></font></p>
        <p><input type="button" value="<?php echo $msg->get('Voltar') ?>" onclick="history.back()" class="button"></p>
<?php
    }
    else
    {
        echo "<br><b>".$msg->get('LUDECOMP_MSG_04')."</b> $dim";
        echo "<br>";
        echo "<p align=left><font face=Arial><b>".$msg->get('LUDECOMP_MSG_05')."</b></font>";
        echo "<table border=\"0\">\n";
        for ($i=0; $i<$dim; $i++) {
            echo "<tr>\n";
            for ($j=0; $j<$dim; $j++) {
                $aux=$j+1;
                if ($j>0) {
                    echo "<td valign=\"top\" align=\"center\" width=\"20\"> + </td>";
                } //end ifj
                echo "<td align=\"right\" valign=\"top\">".$A[$i][$j]."</td>";
                echo "<td align=\"right\" valign=\"top\">x<sub>".$aux."</sub></td>";
            } //end forj
            echo "<td valign=\"top\" align=\"center\" width=\"20\"> = </td>";
            echo "<td valign=\"top\" align=\"right\">$T[$i]</td>";
            echo "\n</tr>\n";
        } //end fori
        echo "</table>";
    
        /* Verifica se o problema pode ser decomposto em LxU */
        $naodecomp = 0;
        for ($aux=1; $aux <= $dim; $aux++)
        {
            if ($aux == 1) //primeira submatriz (1x1);
                $result[0] = $A[0][0];
            else if ($aux == 2) //segunda submatriz (2x2);
                $result[1] = $A[0][0]*$A[1][1] - $A[0][1]*$A[1][0];
            else
            { // para as submatrizes maiores que 2x2
                for ($i=0; $i<$aux; $i++)
                {
                    for ($j=0; $j<$aux; $j++)
                        $B[$i][$j]=$A[$i][$j];
                } //end for i
                $d=1;
                for ($k=0; $k<$aux; $k++)
                {
                    $p=$B[$k][$k];
                    for ($i=0; $i<$aux; $i++)
                        $t[$i]=$B[$i][$k];
                    for ($j=$k; $j<$aux ;$j++)
                    {
                        @$B[$k][$j]/=$p;
                        for ($i=0; $i<$aux; $i++)
                        {
                            if ($k!=$i)
                                $B[$i][$j]-=$t[$i]*$B[$k][$j];
                        } // end for i
                    } //end for j
                    $d = $d * $p; //fator de correcao do determinante
                } //end for k
                for ($i=0; $i<$aux; $i++)
                    $d*=$B[$i][$i];
                $result[$aux-1] = $d;
            } //end else
            if ($result[$aux-1] == 0) $naodecomp = 1;
        } //end for aux
        echo "<br><br><b>".$msg->get('Verificando_Possivel_Executar')."</b><br><br>";
        if ($naodecomp == 1)
        {
            echo "<b>".$msg->get('LUDECOMP_MSG_13')."</b><br><br>";
            echo "\n<script language=\"javascript\" type=\"text/javascript\">\n";
            for ($k=1; $k<=$dim-1; $k++)
            {
                echo "function formulario$k() {\n";
                echo "window.open('calcdet.php', 'submatriz$k', 'toolbar=no, location=no, directories=no, scrollbars=yes,resizable=yes,menubar=no,width=400,height=320');\n";
                echo "document.matriz$k.submit();\n";
                echo "}\n";
            }
        }
            echo "</script>";
            $positiva = 0;
            for ($k=1; $k<=($dim-1); $k++)
            {
                echo "<br>";
                if ($result[$k-1] == 0) { $positiva = 1; }
                if ($k < $aux) { echo "".$msg->get('DECMAT_MSG_11')." A<sub>$k</sub>: "; }
                echo "<br><br>";
                echo "<form action=calcdet.php method=post name=matriz$k target=submatriz$k><table>";
                for ($i=0; $i<$dim ; $i++)
                {
                    echo "<tr>";
                    for ($j=0; $j<$dim ; $j++)
                    {
                        if (($i<$k)and($j<$k))
                        {
                            echo "<td bgcolor=#FFCFAB weight=25 width=25 valign=center align=center cellpading=0 cellspacing=0>";
                            echo "<input type=hidden name=A[$i][$j] value=".$A[$i][$j].">";
                        }
                        else
                            echo "<td weight=25 width=25 valign=center align=center cellpading=0 cellspacing=0>";
                        echo $A[$i][$j];
                        echo "</td>";
                    } //end for
                    echo "</tr>";
                } //end for
                echo "<input type=hidden name=dim value=$k>";
                echo "</table></form>";
                echo "<br><a href=\"javascript:void(0)\" onClick=\"formulario$k()\">";
                if ($k < $aux)
                {
                    echo "<b>".$msg->get('DECMAT_MSG_12')." $k x $k = </b></a>";
                    if ($result[$k-1]>0)
                        echo "<font color=blue>".$result[$k-1]."</font>";
                    else
                        echo "<font color=red>".$result[$k-1]."</font>";
                }
                echo "<br><br><br>";
            }
        //}
        //else
        //{
    
        /**************** FIM DA VERIFICACAO DA POSSIBILDADE DE DECOMPOSICAO **************/
    
        /* inicializar */
        for ($i=0; $i<$dim; $i++) {
            for ($j=0; $j<=$dim; $j++) {
            $L[$i][$j]=0;
            $U[$i][$j]=0;
            } //end for j
        } //end for i
    
        /******************************************** retrosubsstituicao *************************************************************/
                                        //line 90
        function retrosubs($B) {
    
            global $dim,$VetX;
    
            $VetX[$dim-1]=$B[$dim-1][$dim]/$B[$dim-1][$dim-1];
            for ($k=$dim-2; $k>=0; $k--) {
                $VetX[$k]=$B[$k][$dim];
                for ($i=$k+1; $i<$dim; $i++) {
                    $VetX[$k]-=$B[$k][$i]*$VetX[$i];
                } // end for
                $VetX[$k]/=$B[$k][$k];
            } // end for1							//line 100
    
        } // end retrosubs
    
    
        /************************************************ substituicao forward ******************************************************/
    
        function fwsub($f) {
    
            global $dim,$VetY;
    
            $VetY[0]=$f[0][$dim]/$f[0][0];
            for ($i=1; $i<$dim; $i++) {
                $Soma=0;
                for ($j=0; $j<=$i-1; $j++) {
                    $Soma+=$f[$i][$j]*$VetY[$j];
                } // end for
                $VetY[$i]=($f[$i][$dim]-$Soma)/$f[$i][$i];
            } // end for1
    
        }// end fwsub
    
    
        /************************************************** somatrio ****************************************************************/
    
        function somatorio($B,$C,$l1,$c2,$ini,$fim) {
    
            global $soma;
        
            $soma=0;
            for ($p=$ini; $p<=$fim; $p++) {
                $soma=$soma+$B[$l1][$p]*$C[$p][$c2];
            } // end for
        
            return($soma);
    
        } // end somatorio
    
    
        /************************************************** decomposicao *************************************************************/
    
        global $A,$L,$U,$dim,$p,$Lt,$T,$Vy,$VetY;
    
        if (@det()!=0) {
            for ($i=0; $i<$dim; $i++) {
                for ($j=0; $j<$dim; $j++) {
                    if ($i<$j) {
                            $U[$i][$j]=$A[$i][$j]-somatorio($L,$U,$i,$j,0,$i-1);
                    } // end if
                    else {
                        if ($i==$j) {
                            $U[$i][$j]=$A[$i][$j]-somatorio($L,$U,$i,$j,0,$i-1);
                            $L[$i][$j]=($A[$i][$j]-somatorio($L,$U,$i,$j,0,$j-1))/$U[$j][$j];
                        } // end if
                        else {
                            $L[$i][$j]=($A[$i][$j]-somatorio($L,$U,$i,$j,0,$j-1))/$U[$j][$j];
                        } // end else
                    } // end else1
                } // end for
            } // end for1
            for ($h=0; $h<$dim; $h++) {
                for ($p=0; $p<=$dim; $p++) {
                    if ($p==$dim) {
                        $Lt[$h][$p]=$T[$h];
                    } // end if
                    else {
                        $Lt[$h][$p]=$L[$h][$p];
                    } // end else
                } // end for
            } // end for
    
            fwsub($Lt);
    
            for ($h=0; $h<$dim; $h++) {
                for ($p=0; $p<=$dim;$p++) {
                    if ($p==$dim) {
                        $Uy[$h][$p]=$VetY[$h];
                    } // end if
                    else {
                        $Uy[$h][$p]=$U[$h][$p];
                    } // end else
                } // end for
            } // end for
    
            retrosubs($Uy);
    
        /**************************************fim decomposicao********************************************/
        echo "<p align=left><font face=Arial><b>".$msg->get('LUDECOMP_MSG_08')."</b></font>";
        echo "<table border=\"0\">";
        for ($i=0; $i<$dim; $i++) {
            echo "<tr>";
            for ($j=0; $j<$dim; $j++) {
                echo "<td valign=\"top\" align=\"center\" width=\"50\">".round($L[$i][$j],4)."</td>";
            } //end for j
            echo "</tr>";
        } //end for i
        echo "</table>";
        echo "<p align=left><font face=Arial><b>".$msg->get('LUDECOMP_MSG_09')."</b></font>";
        echo "<table border=\"0\">";
        for ($i=0; $i<$dim; $i++) {
            echo "<tr>";
            for ($j=0; $j<$dim; $j++) {
                echo "<td valign=\"top\" align=\"center\" width=\"50\">".round($U[$i][$j],4)."</td>";
            } //end for j
        } //end for i
    
        echo "</table>";
        echo "<p align=left><font face=Arial><b>".$msg->get('LUDECOMP_MSG_11')."</b></font>";
        echo "<br>";
        echo "x = (";
        for ($i=0; $i<$dim; $i++) {
                echo $VetX[$i];
                if ($i!=$dim-1) {echo ",";}
                echo "<input type=hidden name=VetX[".($i)."] maxlength=10 size=5 value=".($VetX[$i]).">";
        } //end fori
        echo ") <sup>T</sup>";
        for  ($i=0; $i<$dim; $i++)
            $T[$i]=$Atemp[$i][$dim];
        seqlsol($Atemp,$VetX,$dim,$T);
        echo "<p>";
        echo "<form method=post enctype=multipart/form-data action=index.php?inc=ludecomp.php>";
        echo "<input class=button type=submit name=Submit value=".$msg->get('G_MSG_03').">";
        echo "</form>";
        } // end if
        else {
        echo "<p align=left><font face=Arial>".$msg->get('LUDECOMP_MSG_10')."</font>";
        echo "<p>";
        echo "<form method=post enctype=multipart/form-data action=index.php?inc=ludecomp.php>";
        echo "<input class=button type=submit name=Submit value=".$msg->get('G_MSG_03').">";
        echo "</form>";
        }  //end else
        //} //else (o problema pode ser decomposto em LxU)
        if (isset($_POST["navegacao"]) && $_POST["navegacao"] == "sim")
        {
            echo "<form name=\"exemplos\" enctype=\"multipart/form-data\" method=\"post\">";
            echo "<script language=\"javascript\" type=\"text/javascript\">\n";
            echo "function criarLinks(value) {\n";
            echo "document.exemplos.indice.value = value;\n";
            echo "document.exemplos.action = 'index.php?inc=ludecomp.php&modo=exemplo'\n";
            echo "document.exemplos.submit();\n";
            echo "}\n";
            echo "</script>";
            echo "<input type=\"hidden\" name=\"id_bib\" value=\"".$id_bib."\">";
            echo "<input type=\"hidden\" name=\"xml_file\" value=\"".$xml_file."\">";
            echo "<input type=\"hidden\" name=\"indice\">";
            echo $msg->get('Outros_Exemplos')."<br>";
            $objeto = new DOMDocument();
            $objeto->load($xml_file);
            $filho = $objeto->getelementsbytagname("lin_systems");
            if ($filho->length > 0)
            {
                    $temp = $filho->item(0)->getelementsbytagname("system");
                    $j = 0;
                for($i = 0; $i < $temp->length; $i++)
                {
                    $method = $temp->item($i)->getelementsbytagname("method");
                    $conteudo = $method->item(0)->nodeValue;
                    $dime = $temp->item($i)->getelementsbytagname("dim");
                    $dime = $dime->item(0)->nodeValue;
                    if ($conteudo == 'direct')
                    {
                        if ($j != 0) echo " | ";
                        if ($j == $indice) echo "<b>";
                        echo "<a href=\"javascript:criarLinks($j)\" title=\"Dimens&atilde;o $dime\"><u>".($j+1)."</u></a>";
                        if ($j == $indice) echo "</b>";
                        $j++;
                    }
                }
            }
            echo "</form>";
        }
        echo $msg->get('G_PROXIMO_EXEMPLO');
        echo "</p>";
        echo "<form method=post enctype=multipart/form-data action=index.php?inc=cholesky.php>";
        if (isset($_POST["navegacao"]) && $_POST["navegacao"] == "sim")
        {
            echo "<input type=\"hidden\" name=\"xml_file\" value=\"".$xml_file."\">";
            echo "<input type=hidden name=navegacao value=sim>";
            echo "<input type=hidden name=indice value=$indice>";
        }
        echo "<input type=hidden name=dim value=".($dim).">";
        for ($i=0; $i<$dim; $i++) {
        for ($j=0; $j<$dim; $j++) {
            echo "<input type=hidden name=A[".($i)."][".($j)."] value=".($Atemp[$i][$j]).">";
        } //end for j
        echo "<input type=hidden name=T[".($i)."] value=".($Atemp[$i][$j]).">";
        } //end for i
        echo "<input class=button type=submit name=Calcular value='".$msg->get('CHOLESKY_MSG_03')."'>";
            echo "</form>";
    
    
        echo "<form method=post enctype=multipart/form-data action=index.php?inc=gauss1.php>";
        if (isset($_POST["navegacao"]) && $_POST["navegacao"] == "sim")
        {
            echo "<input type=\"hidden\" name=\"xml_file\" value=\"".$xml_file."\">";
            echo "<input type=hidden name=navegacao value=sim>";
            echo "<input type=hidden name=indice value=$indice>";
        }
        echo "<input type=hidden name=dim value=".($dim).">";
        for ($i=0; $i<$dim; $i++) {
        for ($j=0; $j<$dim; $j++) {
            echo "<input type=hidden name=A[".($i)."][".($j)."] value=".($Atemp[$i][$j]).">";
        } //end for j
        echo "<input type=hidden name=A[".($i)."][".($j)."] value=".($Atemp[$i][$j]).">";
            } //end for i
        if (@$correcao=="sim") {
        echo "<input type=hidden name=correcao value=sim>";
        for ($i=0; $i<$dim; $i++) {
            echo "<input type=hidden name=vet_resp[".($i)."] value=".($vet_resp[$i]).">";
        } //end fori
        } // end if
        echo "<input class=button type=submit name=Calcular value='".$msg->get('GAUSS_MSG_03a')."'>";
            echo "</form>";
    
        echo "<form method=post enctype=multipart/form-data action=index.php?inc=gauss2.php>";
        if (isset($_POST["navegacao"]) && $_POST["navegacao"] == "sim")
        {
            echo "<input type=\"hidden\" name=\"xml_file\" value=\"".$xml_file."\">";
            echo "<input type=hidden name=navegacao value=sim>";
            echo "<input type=hidden name=indice value=$indice>";
        }
        echo "<input type=hidden name=dim value=".($dim).">";
        for ($i=0; $i<$dim; $i++) {
        for ($j=0; $j<$dim; $j++) {
            echo "<input type=hidden name=A[".($i)."][".($j)."] value=".($Atemp[$i][$j]).">";
        } //end for j
        echo "<input type=hidden name=A[".($i)."][".($j)."] value=".($Atemp[$i][$j]).">";
            } //end for i
        if (@$correcao=="sim") {
        echo "<input type=hidden name=correcao value=sim>";
        for ($i=0; $i<$dim; $i++) {
            echo "<input type=hidden name=vet_resp[".($i)."] value=".($vet_resp[$i]).">";
        } //end fori
        } // end if
        echo "<input class=button type=submit name=Calcular value='".$msg->get('GAUSS_MSG_03b')."'>";
        echo "</form>";
    }
}
?>
</div>